package com.example.schedule.Controller;


import com.example.schedule.Service.LoginService;
import com.example.schedule.domain.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;

@Controller
public class HomeController {

    @Autowired
    private LoginService loginService;
    @GetMapping("/home")
    public String home(){
         return "index.html";
    }

    @PostMapping("/login")
    public String login(@RequestParam String username, @RequestParam String password, HttpServletRequest request){
        User user = loginService.login(username, password);

        if(user != null){
            request.getSession().setAttribute("user",user);
            return "brand";
        }
        return "register.html";
    }

    @PostMapping("/register")
    public String register(@RequestParam String email,@RequestParam  String username,@RequestParam  String password){
        // 处理字符串  封装成 user 对象
        User user = new User();
        user.setId(7);
        user.setUsername(username);
        user.setPassword(password);
        user.setEmail(email);
        user.setHeadUrl("/schedule");
        user.setCreateTime("2020/9/25");
//        long startTime = System.currentTimeMillis();
//        Timestamp now1 = new Timestamp(startTime);
//        System.out.println("now1:" + now1);
        loginService.add(user);
        return "index";
    }


    



}
